package ynu.sxp.demoapp.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ynu.sxp.demoapp.entity.OrderDetailet;
import ynu.sxp.demoapp.repository.OrderDetailetRepository;
import ynu.sxp.demoapp.service.OrderDetailetService;

import java.util.List;
import java.util.UUID;

@Service
public class OrderDetailetServiceImpl implements OrderDetailetService {
    private final OrderDetailetRepository orderDetailetRepository;

    @Autowired
    public OrderDetailetServiceImpl(OrderDetailetRepository orderDetailetRepository) {
        this.orderDetailetRepository = orderDetailetRepository;
    }

    @Override
    @Transactional
    public OrderDetailet createOrderDetailet(OrderDetailet orderDetailet) {
        return orderDetailetRepository.save(orderDetailet);
    }

    @Override
    public OrderDetailet getOrderDetailetById(UUID odId) {
        return orderDetailetRepository.findById(odId).orElse(null);
    }

    @Override
    public List<OrderDetailet> getOrderDetailesByOrderId(UUID orderId) { // 修改为 UUID 类型
        return orderDetailetRepository.findByOrderId(orderId);
    }

    @Override
    @Transactional
    public void updateOrderDetailet(OrderDetailet orderDetailet) {
        orderDetailetRepository.save(orderDetailet);
    }

    @Override
    @Transactional
    public void deleteOrderDetailet(UUID odId) {
        orderDetailetRepository.deleteById(odId);
    }

    @Override
    @Transactional
    public List<OrderDetailet> createOrderDetailes(List<OrderDetailet> orderDetailes) {
        return orderDetailetRepository.saveAll(orderDetailes);
    }

    @Override
    @Transactional
    public void deleteOrderDetailes(List<UUID> odIds) {
        orderDetailetRepository.deleteAllById(odIds);
    }
}